package com.qf.business.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qf.data.user.entity.RedEnvelopes;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * (RedEnvelopes)表数据库访问层
 *
 * @author makejava
 * @since 2022-07-20 10:21:24
 */
public interface RedEnvelopesMapper extends BaseMapper<RedEnvelopes> {

    @Select("select r.*," +
            "(r.score - if(count(rd.id) > 0, sum(rd.score), 0)) as hasScore," +
            "(r.count - count(rd.id)) as hasCount " +
            "from `red_envelopes` r left join `red_envelopes_details` rd on r.id = rd.rid " +
            "where r.id = #{redid}")
    RedEnvelopes queryRedEnvelopesDetails(long redid);

    @Update("update red_envelopes set status = #{status} where id = #{redid}")
    int updateStatus(@Param("redid") long redid, @Param("status") int status);

    @Select("select r.*, (r.score - ifnull(sum(rd.score), 0)) as hasScore from `red_envelopes` r left join `red_envelopes_details` rd" +
            " on r.id = rd.rid" +
            " where r.status = 0 and r.timeout < now()")
    List<RedEnvelopes> queryTimeoutRed();
}


